# -*- coding: utf-8 -*-

"""
    本文件用于验证fxj数据解码的正确性
"""

import unittest
from wolfox.foxit.base.common import Quote
from wolfox.foxit.source import reader,filestore,fxj

import os.path
cur_dir = os.path.dirname(__file__)
#test_dir = os.path.split(cur_dir)[0]
#test_data_dir = os.path.join(test_dir,'data')
test_data_dir = os.path.join(cur_dir,'test_data')

class FxjTest(unittest.TestCase):
    def testDay(self):
        SRCNAME = os.path.join(test_data_dir,"quote_test2.dad")
        DESNAME = os.path.join(test_data_dir,"output/quote_day_test.txt")
        dayextractor = fxj.TradeExtractor()
        fxjreader = reader.Reader(dayextractor)
        records = fxjreader.read(SRCNAME)
        self.assertEquals(37,len(records))        
        #print len(records)
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()

    def testMin5(self):
        SRCNAME = os.path.join(test_data_dir,"m5_test.dad")
        DESNAME = os.path.join(test_data_dir,"output/m5_test.txt")
        m5extractor = fxj.TradeExtractor(fxj.min5_func)
        fxjreader = reader.Reader(m5extractor)
        records = fxjreader.read(SRCNAME)
        #print len(records)
        self.assertEquals(195,len(records))
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()

    def testM5to60(self):
        SRCNAME = os.path.join(test_data_dir,"m5_test.dad")
        DESNAME = os.path.join(test_data_dir,"output/m5to60_test.txt")
        #SRCNAME = "../data/200908m.dad"
        #DESNAME = os.path.join(test_data_dir,"200908m.txt")
        m5extractor = fxj.M5to60Extractor()
        fxjreader = reader.Reader(m5extractor)
        records = fxjreader.read(SRCNAME)
        #self.assertEquals(17,len(records))
        #print len(records)
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()

    def testXInfo(self):
        SRCNAME = os.path.join(test_data_dir,"xtest.pwr")
        DESNAME = os.path.join(test_data_dir,"output/xtest.pow")
        extractor = fxj.XExtractor()
        fxjreader = reader.Reader(extractor)
        records = fxjreader.read(SRCNAME)
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()
 
    def testReport(self):
        SRCNAME = os.path.join(test_data_dir,"report_test.fin")
        DESNAME = os.path.join(test_data_dir,"output/report_test.rpt")
        extractor = fxj.ReportExtractor()
        fxjreader = reader.Reader(extractor)
        records = fxjreader.read(SRCNAME)
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()
    
    def testCatalog(self):
        SRCNAME = os.path.join(test_data_dir,"xizang.BLK")
        DESNAME = os.path.join(test_data_dir,"output/xizang.txt")
        extractor = fxj.CatalogExtractor()
        fxjreader = reader.Reader(extractor)
        records = fxjreader.read(SRCNAME)
        fs = filestore.CSVStore()    
        fs.open(DESNAME)
        try:
            fs.save(records)
        finally:
            fs.close()


class CodeReaderTest(unittest.TestCase):
    
    def test_find_type(self):
        reader = fxj.CodeReader()
        self.assertEquals(None,reader.find_type('SHX00120'))
        self.assertEquals(u'股票',reader.find_type('SH600120'))
        self.assertEquals(u'基金',reader.find_type('SH500020'))

    def test_parse(self):
        reader = fxj.CodeReader()        
        self.assertEquals(('SH','SH000001',u'上证指数',u'指数'),reader.parse(u'000001\t上证指数\n'.encode('gbk'),'SH'))

    def test_readlines(self):   #通透测试
        reader = fxj.CodeReader()
        src_name = os.path.join(test_data_dir,"SZ.SNT")
        reader.readlines(src_name)
        self.assertTrue(True)
        
    def test_read_codes(self):  #通透测试
        reader = fxj.CodeReader()
        src_name = os.path.join(test_data_dir,"SZ.SNT")
        results = reader.read_codes(src_name)
        self.assertEquals(6,len(results))


class CatalogReaderTest(unittest.TestCase):

    def test_read_catalogs(self):
        creader = fxj.CatalogReader()
        results = creader.read_catalogs(os.path.join(test_data_dir,'blk'))
        self.assertEquals(3,len(results))
        names = [ name for name,members in results ]
        self.assertTrue(u'广西' in names)
        self.assertTrue(u'广东' in names)
        self.assertTrue(u'贵州' in names)        


if __name__ == "__main__":
    import logging
    logging.basicConfig(filename="test.log",level=logging.DEBUG,format='%(name)s:%(funcName)s:%(lineno)d:%(asctime)s %(levelname)s %(message)s')
    unittest.main()
